package com.lin.carbon.common;

import lombok.Getter;
import lombok.Setter;

import java.io.Serial;
import java.io.Serializable;

/**
 * 统一后端响应类
 * @param <T> 响应数据的类型
 */
@Setter
@Getter
public class Result<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    // 状态码
    private int code;
    // 消息
    private String message;
    // 响应数据
    private T data;

    public Result() {}

    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    // 成功响应，带数据
    public static <T> Result<T> success(T data) {
        return new Result<>(200, "操作成功", data);
    }

    // 成功响应，不带数据
    public static <T> Result<T> success() {
        return new Result<>(200, "操作成功", null);
    }

    // 失败响应，带自定义消息
    public static <T> Result<T> error(String message) {
        return new Result<>(500, message, null);
    }

    // 失败响应，带自定义状态码和消息
    public static <T> Result<T> error(int code, String message) {
        return new Result<>(code, message, null);
    }

}
